// 课程相关的逻辑控制单元
// 引入数据库文件
const number = require("@hapi/joi/lib/types/number");
const db = require("../config/db");

/**
 * 课程查询接口逻辑
 * @param {*} req
 * @param {*} res
 */
exports.listCourseController = (req, res) => {
  // 1、接收数据
  let { category, page, size } = req.query;
  console.log(category, page, size);
  page = (page - 1) * size; // 每页从第几个开始显示
  console.log(page);
  // 2、处理逻辑
  // 查询课程表中的数据，根据分类查询，按照id排序，每页显示limit
  const pageSql =
    "select * from course where category = ? and del= 0 order by id limit ?,?";
  // 总个数
  const totalSql = "select count(*) as total from course where category = ? and del= 0";
  // 执行查询分页的数据
  db.query(pageSql, [category, Number(page), Number(size)], (err, resPage) => {
    if (err) {
      return res.send({
        code: 1,
        msg: "课程查询失败",
      });
    }
    // 执行查询总数的SQL
    db.query(totalSql, [category], (err, resTotal) => {
      if (err) {
        return res.send({
          code: 1,
          msg: "总数查询失败",
        });
      }
      console.log(resTotal[0].total);
      // 3、响应数据
      res.send({
        code: 0,
        msg: "课程查询成功",
        data: {
          list: resPage,
          total: resTotal[0].total,
        },
      });
    });
  });
};

/**
 * 根据Id查询某一个课程详情的接口逻辑
 * @param {*} req 
 * @param {*} res 
 */
exports.findCourseByIdController = (req, res) => {
    // 1、接收数据
    const { id} = req.query;
    console.log(id);
    // 2、处理数据
    const findByIdSql = 'select * from course where id = ?'
    db.query(findByIdSql, id, (err, results) => {
        if(err){
            return res.send({
                code: 1,
                msg: '根据ID查询课程失败'
            })
        }
        // 3、响应数据
        res.send({
            code: 0,
            msg: '根据ID查询课程成功',
            data: results
        })
    })
}

/**
 * 课程修改接口逻辑(包括修改删除状态)
 * @param {*} req
 * @param {*} res
 */
exports.updateCourseController = (req, res) => {
  // 1、接收数据
  const { title, price, point, course_img, id, del } = req.body;
  console.log(title, price, point, course_img, id, del);
  let sql = "update course set ";
  let values = [];
  // 构建WHERE条件
  let whereConditions = [];
  // 2、处理数据
  // 判断数据是否为空
  if (title) {
    whereConditions.push(" title = ? ");
    values.push(title);
  }
  if (course_img) {
    whereConditions.push(" course_img = ? ");
    values.push(course_img);
  }
  if (price) {
    whereConditions.push(" price = ? ");
    values.push(Number(price));
  }
  if (point) {
    whereConditions.push(" point = ? ");
    values.push(Number(point));
  }
  if (del) {
    whereConditions.push(" del = ? ");
    values.push(Number(del));
  }
  console.log("whereConditions长度:", whereConditions.length);
  if (whereConditions.length > 0) {
    sql += whereConditions.join(" , ");
    console.log("SQL语句：", sql);
    console.log("values：", values);
  }
  sql += " WHERE " + " id = ? ";
  values.push(Number(id));
  console.log("最终SQL语句：", sql);
  console.log("最终values：", values);
  db.query(sql, values, (err, resUpdate) => {
    if (err) {
      return res.send({
        code: 1,
        msg: "课程修改失败",
      });
    }
    // 3、响应数据
    res.send({
      code: 0,
      msg: "课程修改成功"
    });
  });
};

/**
 * 删除课程接口逻辑
 * @param {*} req 
 * @param {*} res 
 */
exports.deleteCourseController = (req, res) => {
    // 1、接收数据
    const { id } = req.query;
    console.log(id);
    // 2、处理数据
    const deleteSql = 'update course set del = 1 where id = ?'
    db.query(deleteSql, id, (err, results) => {
        if(err){
            return res.send({
                code: 1,
                msg: '删除课程失败'
            })
        }
        // 3、响应数据
        res.send({
            code: 0,
            msg: '删除课程成功'
        })
    })
}
